package com.ymatou.iapi.fenqile.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.fenqile.parameter.SyncFqlMemberInfoBean;
import com.ymatou.iapi.fenqile.service.SyncFqlMemberInfoCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.FqlMember;
import com.ymttest.database.model.FqlMemberExample;
import com.ymttest.database.sqlwapper.PayGateWayWapper;
/**
*分期乐同步会员信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SyncFqlMemberInfo{
	private static SyncFqlMemberInfoBean syncfqlmemberinfoBean;
	private static SyncFqlMemberInfoCall syncfqlmemberinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分期乐同步会员信息");
	}

	@Before
	public void caseUp() {
		syncfqlmemberinfoBean=new SyncFqlMemberInfoBean();
		syncfqlmemberinfoCall=new SyncFqlMemberInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(syncfqlmemberinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SyncFqlMemberInfo_001() {
		Logger.start(true,"分期乐同步会员信息");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			//获取openid
			String open_id=PayGateWayCallService.CodeFin(uid).get("MockResult-FqlPay-open_id").toString();
			Thread.sleep(3000);
			Map mcredit = PayGateWayCallService.CreditFin(uid, open_id);
			
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mcredit.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mcredit.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");
			
			HashMap mockHeader=(HashMap) PayGateWayCallService.CreditMockMap();
//			mockHeader.put("mock", 1);
			mockHeader.put("MockResult-FqlPay-new_user_flag", 3);
			mockHeader.put("MockResult-FqlPay-max_overdue", 4);
			mockHeader.put("MockResult-FqlPay-total_credit", 9999);
			mockHeader.put("MockResult-FqlPay-total_avl_credit", 9998);
			mockHeader.put("MockResult-FqlPay-total_un_repay_amount", 1);
			PayGateWayCallService.addMock(syncfqlmemberinfoCall, mockHeader);
			syncfqlmemberinfoBean.setUserId(uid);
			syncfqlmemberinfoCall.setData(syncfqlmemberinfoBean);
			syncfqlmemberinfoCall.callService();
			Logger.verifyEquals("ok",syncfqlmemberinfoCall.getReturnData() , "验证返回");
			Thread.sleep(2000);
			dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SyncFqlMemberInfo_002() {
		Logger.start(true,"同步分期乐用户信息成功-同一个fqlopenid被2个码头账户绑定,第二次绑定时也刷第一次的账号");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			
			UserRegisterInfoRequestDtoBean regbean2 = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc2 = JUserCallService.UserRegisterInfoRequestDtoCall(regbean2);
			JsonObject registerResult2 = urrc2.getJsonObject().get("Result").getAsJsonObject();
			String token2=registerResult2.get("Token").getAsString();
			int uid2  = urrc2.getUserid();
			
			//获取openid
			String open_id=PayGateWayCallService.CodeFin(uid).get("MockResult-FqlPay-open_id").toString();
			Thread.sleep(3000);
			Map mcredit = PayGateWayCallService.CreditFin(uid, open_id);
			
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mcredit.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mcredit.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");
			
			HashMap mockHeader=(HashMap) PayGateWayCallService.CreditMockMap();
			mockHeader.put("MockResult-FqlPay-new_user_flag", 3);
			mockHeader.put("MockResult-FqlPay-max_overdue", 4);
			mockHeader.put("MockResult-FqlPay-total_credit", 9999);
			mockHeader.put("MockResult-FqlPay-total_avl_credit", 9998);
			mockHeader.put("MockResult-FqlPay-total_un_repay_amount", 1);
			PayGateWayCallService.addMock(syncfqlmemberinfoCall, mockHeader);
			syncfqlmemberinfoBean.setUserId(uid);
			syncfqlmemberinfoCall.setData(syncfqlmemberinfoBean);
			syncfqlmemberinfoCall.callService();
			Logger.verifyEquals("ok",syncfqlmemberinfoCall.getReturnData() , "验证返回");
			Thread.sleep(2000);
			dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");
			
			
			PayGateWayCallService.CodeFin(uid2,open_id).get("MockResult-FqlPay-open_id").toString();
			Thread.sleep(3000);
			Map mcredit2 = PayGateWayCallService.CreditFin(uid2, open_id);
			mockHeader=(HashMap) PayGateWayCallService.CreditMockMap();
			mockHeader.put("MockResult-FqlPay-new_user_flag", 3);
			mockHeader.put("MockResult-FqlPay-max_overdue", 2);
			mockHeader.put("MockResult-FqlPay-total_credit", 3);
			mockHeader.put("MockResult-FqlPay-total_avl_credit", 4);
			mockHeader.put("MockResult-FqlPay-total_un_repay_amount", 5);
			PayGateWayCallService.addMock(syncfqlmemberinfoCall, mockHeader);
			syncfqlmemberinfoBean.setUserId(uid2);
			syncfqlmemberinfoCall.setData(syncfqlmemberinfoBean);
			syncfqlmemberinfoCall.callService();
			Logger.verifyEquals("ok",syncfqlmemberinfoCall.getReturnData() , "验证返回");
			dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");
			
			
			example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid2);
			dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SyncFqlMemberInfo_003() {
		Logger.start(true,"同步分期乐用户信息成功-查询核身状态");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int uid  = urrc.getUserid();
			//获取openid
			String open_id=PayGateWayCallService.CodeFin(uid).get("MockResult-FqlPay-open_id").toString();
			Thread.sleep(3000);
			Map mcredit = PayGateWayCallService.CreditFin(uid, open_id);
			
			FqlMemberExample example=new FqlMemberExample();
			example.createCriteria().andUserIdEqualTo(uid);
			List<FqlMember> dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mcredit.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mcredit.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mcredit.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");
			
			HashMap mockHeader=(HashMap) PayGateWayCallService.CreditMockMap();
			mockHeader.put("MockResult-FqlPay-new_user_flag", 3);
			mockHeader.put("MockResult-FqlPay-max_overdue", 4);
			mockHeader.put("MockResult-FqlPay-total_credit", 9999);
			mockHeader.put("MockResult-FqlPay-total_avl_credit", 9998);
			mockHeader.put("MockResult-FqlPay-total_un_repay_amount", 1);
			mockHeader.put("MockResult-FqlPay-check_identity", 1);
			PayGateWayCallService.addMock(syncfqlmemberinfoCall, mockHeader);
			syncfqlmemberinfoBean.setUserId(uid);
			syncfqlmemberinfoCall.setData(syncfqlmemberinfoBean);
			syncfqlmemberinfoCall.callService();
			Logger.verifyEquals("ok",syncfqlmemberinfoCall.getReturnData() , "验证返回");
			Thread.sleep(2000);
			dbs = new PayGateWayWapper().selectFqlMemberExample(example);
			Logger.verifyEquals(1, dbs.size(), "验证数据进入数据库");
			Logger.debug(FormatUtil.beanToGSONString(dbs));
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-max_overdue"), dbs.get(0).getMaxOverDue(), "验证MaxOverDue");
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-new_user_flag"), dbs.get(0).getNewUserFlag(), "验证NewUserFlag");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_credit").toString()).setScale(2), dbs.get(0).getTotalCredit().setScale(2), "验证totalCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_avl_credit").toString()).setScale(2), dbs.get(0).getAvailableCredit().setScale(2), "验证availableCredit");
			Logger.verifyEquals(new BigDecimal(mockHeader.get("MockResult-FqlPay-total_un_repay_amount").toString()).setScale(2), dbs.get(0).getUnReplayAmount().setScale(2), "验证unReplayAmount");
			Logger.verifyEquals(mockHeader.get("MockResult-FqlPay-check_identity"), dbs.get(0).getCheckIdentity(), "验证MaxOverDue");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}